
import { _decorator, Component } from 'cc';
import * as cc from 'cc';
declare const CC_EDITOR
const { ccclass, property, executeInEditMode, playOnFocus } = _decorator;
/** 效果类型 */
enum EffectType {
    none = 0,   //无
    scale = 1,  //缩放
    scaleToPoint = 2,    //缩放到某点
    centerUnfolding = 3,   //从中部展开
}
/**
 * 界面打开 和 关闭效果
 * 组件请挂载到对应ui界面上
 * @author 神兽白泽
 * @description 交流群：415468592
 * executeInEditMode 3.x暂时不支持 tween
 */
@ccclass("UIOpenAndClose")
@executeInEditMode()
@playOnFocus()
export default class UIOpenAndClose extends cc.Component {

    @property({ type: cc.Node, displayName: '动画节点' })
    private node_tween: cc.Node = null

    @property({ type: cc.Enum(EffectType), displayName: '打开动画节点效果', visible() { return this.node_tween } })
    private open_effect: EffectType = EffectType.none;

    @property({ type: cc.Enum(EffectType), displayName: '关闭动画节点效果', visible() { return this.node_tween } })
    private close_effect: EffectType = EffectType.none;

    @property({ displayName: '关闭节点缩小到此节点坐标', visible() { return this.close_effect == EffectType.scaleToPoint } })
    private close_toPoint: string = "";

    /** 编辑器预览 */
    private _edit_play: boolean = false;
    @property({ displayName: '编辑器预览' })
    get edit_play(): boolean {
        if (this._edit_play && !this.edit_playing) this.playShow();
        this.edit_playing = this._edit_play;
        return this._edit_play;
    }
    set edit_play(v: boolean) {
        this._edit_play = v;
        if (!v) this.stop();
    }

    /** 编辑器是否正在播放 */
    private edit_playing = false;

    onLoad() {
        // this.node.on('ui-close', this.playClose, this);// 界面关闭事件
    }

    start() {
        if (CC_EDITOR) {
            if (this.edit_play) {
                this.playShow()
            }
        } else {
            this.playShow()
        }
    }

    /**
     * 界面打开效果
     */
    private playShow() {
        if (this.node_tween) {
            switch (this.open_effect) {
                case EffectType.none:
                    break;
                case EffectType.scale:
                    this.node_tween.setScale(0, 0, 0);
                    cc.tween(this.node_tween)
                        .to(0.15, { scale: new cc.Vec3(1, 1, 1) })
                        .start()
                    break;
                case EffectType.centerUnfolding:
                    this.node_tween.setScale(0, 1, 1);
                    cc.tween(this.node_tween)
                        .to(0.15, { scale: new cc.Vec3(1, 1, 1) })
                        .start()
                    break;
            }
        }
    }

    /**
     * 关闭打开效果
     * @param cb 结束回调
     */
    public playClose(cb: Function = null) {
        if (this.node_tween) {
            switch (this.close_effect) {
                case EffectType.none:
                    cb && cb();
                    break;
                case EffectType.scale:
                    cc.tween(this.node_tween)
                        .to(0.15, { scale: new cc.Vec3(0, 0, 0) })
                        .call(() => {
                            cb && cb();
                        })
                        .start()
                    break;
                case EffectType.scaleToPoint:
                    // if (this.close_toPoint == "") {
                    //     console.error('需要配置缩小到终点节点路径')
                    // } else {
                    //     let path = this.close_toPoint;
                    //     let node = cc.find(path);
                    //     if (node) {
                    //         let world_pos = node.parent.convertToWorldSpaceAR(node.getPosition());
                    //         let node_pos = this.node_tween.parent.convertToNodeSpaceAR(world_pos);
                    //         cc.tween(this.node_tween)
                    //             .to(0.3, { scale: 0, position: new cc.Vec3(node_pos.x, node_pos.y, 0) })
                    //             .call(() => {
                    //                 cb && cb();
                    //             })
                    //             .start()
                    //     } else {
                    //         cb && cb();
                    //         cc.error('关闭时动画节点未找到，请检查！！！！！')
                    //     }
                    // }
                    break;
            }
        } else {
            cb && cb();
        }
    }

    /** 停止动画 */
    private stop() {
        cc.Tween.stopAllByTarget(this.node_tween);
        this.node_tween.setScale(1, 1, 1)
    }
}